<%
prjs = WtProjectOrders.find(:all, :order=>"dsp_pos", :conditions=>"uid=-1")
tickets = WtTicketRelay.find(:all, :order=>"position")
tic_max = tickets.size

prjs.each do |po|
 dsp_prj = po.dsp_prj
 dsp_pos = po.dsp_pos
 next unless @prj_cost.key?(dsp_prj)
 next unless @prj_cost[dsp_prj].key?(-1)
 prj =Project.find(dsp_prj)
%>
<tr style="background:#000;color:#fff;">
  <td> 
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="JavaScript:prj_pos('<%=url_for(@link_params)%>', <%=dsp_prj%>,<%=dsp_pos%>,<%=prjs.size%>);">
      <%=dsp_pos%>
    </a>
<%end%>
   <%=prj.name%>
  </td>
  <td> <%=@prj_cost[dsp_prj][-1]%> </td>
  <td><b><%=@r_prj_cost[dsp_prj][-1]%></b></td>
  <td>&nbsp</td>
</tr>
<%
 tic_num = 0
 tickets.each do |tic|
  issue_id = tic.issue_id
  tic_num += 1
  if tic.position != tic_num then # 番号が間違っていたらつけなおし
    tic.position = tic_num; tic.save
  end

  # 当該チケットに集計が無かったらスキップ
  next unless @issue_cost.key?(issue_id)
  next unless @issue_cost[issue_id].key?(-1)

  issue = Issue.find_by_id(issue_id)
  next if issue.nil? # チケットが削除されていたらパス
  next if issue.project_id != dsp_prj
  issueHtml = issue.closed? ? "<del>"+issue.to_s+"</del>" : issue.to_s

  if tic.parent && tic.parent != 0 then
    parent = Issue.find_by_id(tic.parent)
    if parent.nil? then
      parentHtml = "<del>Issue:"+tic.parent.to_s+"</del>"
    else
      parentHtml = parent.closed? ? "<del>"+parent.to_s+"</del>" : parent.to_s
    end
  else
    parentHtml = ""
  end

  if @issue_cost.key?(issue_id) && 
     @issue_cost[issue_id].key?(-1) && 
     @issue_cost[issue_id][-1]!=0 then
    cost=sprintf("%.2f",@issue_cost[issue_id][-1])
  else
    cost="&nbsp"
  end

  if @r_issue_cost.key?(issue_id) && 
     @r_issue_cost[issue_id].key?(-1) && 
     @r_issue_cost[issue_id][-1]!=0 then
    if @issue_cost[issue_id][-1]!=@r_issue_cost[issue_id][-1] then
      r_cost=sprintf("<b>%.2f</b>",@r_issue_cost[issue_id][-1])
    else
      r_cost=sprintf("%.2f",@r_issue_cost[issue_id][-1])
    end
  else
    r_cost="&nbsp"
  end

%>
<tr>
  <td>
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="JavaScript:ticket_pos('<%=url_for(@link_params)%>', <%=issue_id%>,<%=tic.position%>,<%=tic_max%>);">
      <%=tic.position%>
    </a>
<%end%>
    <%= link_to(issueHtml, {:controller=>"issues", :action=>"show", :id=>issue.id}, :popup=>true) %>
    <%= print_issue_cost(issue) %>
  </td>
  <td><%=cost%></td>
  <td><%=r_cost%></td>
  <td id="ticket<%=issue_id%>"> => <%=parentHtml%>
<%
if User.current.allowed_to?(:edit_work_time_total, @project) then
  if parentHtml != "" then
%>
<a href="JavaScript:set_ticket_relay('<%=url_for(:action=>"popup_select_ticket")%>',
                                  '<%=url_for(@link_params.merge(:action=>"ajax_relay_table"))%>',
                                  <%=tic.issue_id%>);">[<%=l(:button_change)%>]</a>
<a href="#" onclick="JavaScript:new Ajax.Updater('ticket'+<%=issue_id%>,
  '<%=url_for(@link_params.merge(:action=>"ajax_relay_table", :ticket_relay=>tic.issue_id.to_s+"_0"))%>',
  {asynchronous:true, method:'get'}); return false;">
[<%=l(:button_delete)%>]
</a>

<%else%>
<a href="JavaScript:set_ticket_relay('<%=url_for(:action=>"popup_select_ticket")%>',
                                  '<%=url_for(@link_params.merge(:action=>"ajax_relay_table"))%>',
                                  <%=tic.issue_id%>);">[<%=l(:button_add)%>]</a>
<%
  end
end
%>
</td>
</tr>
<%
 end
end
%>
